package com.syni.mdd.sjb.common.datatreaure.quartz;

import com.syni.mdd.sjb.common.component.mybatis.dxshapp.entity.DxshAppServiceOrder;
import com.syni.mdd.sjb.common.component.utils.common.utils.DateTimeUtils;
import com.syni.mdd.sjb.common.datatreaure.service.DxshAppServiceOrderService;
import com.syni.mdd.sjb.common.datatreaure.service.DxshAppUserChannelService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.Date;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

@Component
@EnableScheduling
public class ServerQuartz {

    private final Integer WAIT_SERVE_FLAG = 2;  //未开始状态
    private final Integer IS_SERVE_FLAG = 3;  //服务中心状态
    private final Integer SERVE_FINISH_FLAG = 4;  //已结束状态

    @Autowired
    DxshAppServiceOrderService dxshAppServiceOrderService;

    @Autowired
    DxshAppUserChannelService dxshAppUserChannelService;

    public static Set<Integer> businessIdsRecord = new HashSet<>();

    /**
     * 服务订单状态变更
     */
    @Scheduled(cron = "0 0 0 * * ?")
    public void changPayStatus(){
        List<DxshAppServiceOrder> orderList = dxshAppServiceOrderService.onlineAddTypeOrder();
        for(DxshAppServiceOrder order : orderList){
            String serveBeginTime = order.getServeBeginTime();
            String serveEndTime = order.getServeEndTime();
            if(serveBeginTime != null && serveEndTime != null){
                Date BT = DateTimeUtils.converStringToDate(serveBeginTime);
                Date ET = DateTimeUtils.converStringToDate(serveEndTime);
                int l = BT.compareTo(new Date());
                int r = ET.compareTo(new Date());
                if(l == 1){
                    order.setPayStatus(WAIT_SERVE_FLAG);
                }
                if(r == -1){
                    order.setPayStatus(SERVE_FINISH_FLAG);
                }
                if ((l == -1 || l == 0) && (r == 1 || r == 0)) {
                    order.setPayStatus(IS_SERVE_FLAG);
                }
            }
        }
        dxshAppServiceOrderService.saveOrUpdateBatch(orderList);
    }

    /**
     * 服务订单状态变更
     */
    @Scheduled(cron = "0 0 0/2 * * ?")
    public void synchronizeData(){
        dxshAppUserChannelService.syncDxshappCompMe();
    }
}
